package dp.proxy;

import java.util.Random;

/**
 * 
 * 为其它对象提供一种代理以控制对这个对象的访问。
 * 被代理对象由代理对象在编译时确定，并且代理对象可能限制对被代理对象的访问。
 * 
 * @author anan
 *
 */
public class ProxySubject implements ISubject {
	private ISubject subject;

	public ProxySubject() {
		subject = new ConcreteSubject();
	}

	@Override
	public void action() {
		preAction();
		if ((new Random()).nextBoolean()) {
			subject.action();
		} else {
			System.out.println("Permission denied");
		}
		postAction();
	}

	private void preAction() {
		System.out.println("ProxySubject.preAction()");
	}

	private void postAction() {
		System.out.println("ProxySubject.postAction()");
	}

	public static void main(String[] args) {
		ISubject subject = new ProxySubject();
		subject.action();
	}
}